<?php

namespace app\admin\controller\forward;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 用户充值提现明细表
 *
 * @icon fa fa-circle-o
 */
class Forward extends Backend
{
    
    /**
     * UserEcharge模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Recharge');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['full_name']=$filters['full_name'];
            }
            $total = $this->model
                ->where($where)
                ->where('process_type','=',1)
                ->order($sort, $order)
                ->group('user_id')
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->where('process_type','=',1)
                ->group('user_id')
                ->select();
            $lists=$this->model->lists($list);
            $result = array("total" => $total, "rows" => $lists);
            return json($result);
        }
        $btns = ['refresh'];
        $this->view->assign('btns', $btns);

        return $this->view->fetch();
    }
    public function lists($ids){
        $row = $this->model
            ->alias('a')
            ->join('users u','u.user_id=a.user_id')
            ->field('a.*,u.nickname,u.user_money')
            ->where(['a.user_id' => $ids,'a.process_type'=>1])->select();
        if (!$row) {
            $this->error('查询信息不存在');
        }
        $this->view->assign("row", $row);
        $this->view->assign("ids", $ids);
        $this->assign('ye',$row[0]['user_money']);
        return $this->view->fetch('list');
    }
    public function export(){
        $ids=input('ids');
        //1.从数据库中取出数据
        $list = $this->model
            ->alias('a')
            ->join('users u','u.user_id=a.user_id')
            ->field('a.*,u.nickname,u.user_money')
            ->where(['a.user_id' => $ids,'a.process_type'=>1])->select();
        $price=0;
        $ye=0;
        $count=count($list);
        $name=$list[0]['nickname'];
        foreach($list as $v){
            $price+=$v['amount'];
            $ye=$v['user_money'];
        }
        //2.加载PHPExcle类库
        vendor('PHPExcel.PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'user_id')
            ->setCellValue('B1', '微信昵称')
            ->setCellValue('C1', '充值金额')
            ->setCellValue('D1', '充值时间')
            ->setCellValue('E1', '订单号');

        //设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($list);$i++){
            $list[$i]['add_time']=date('Y-m-d',$list[$i]['add_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['user_id']);//添加ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['nickname']);//添加姓名
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['amount']);//添加年龄
            $objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['add_time']);//添加班级
            $objPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$list[$i]['trade_no']);//添加班级
        }
        $objPHPExcel->getActiveSheet()->setCellValue('A'.($count+2),'总计');//添加年龄
        $objPHPExcel->getActiveSheet()->setCellValue('B'.($count+2),$price);//添加班级
        $objPHPExcel->getActiveSheet()->setCellValue('C'.($count+2),'余额');//添加班级
        $objPHPExcel->getActiveSheet()->setCellValue('D'.($count+2),$ye);//添加班级
        //7.设置保存的Excel表格名称
        $filename = $name.'充值明细'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle($name.'充值明细');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$filename.'"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;
    }
}
